#!/bin/sh
###############################################################################
##  Author    : xuezhoyi
##  Name      : edw_parm_refresh.sh
##  Functions : Refresh parm file
##  Purpose   : Refresh parm file
##  Revisions or Comments
##  VER        DATE        AUTHOR           DESCRIPTION
##---------  ----------  ---------------  ------------------------------------ 
##  1.0      2017-05-22  xuezhouyi        1. CREATED THIS SHELL.
###############################################################################

. ${0%`basename ${0}`}edw_parm.sh
. ${0%`basename ${0}`}edw_func.sh

function USAGE(){
    echo -e "\nHow to use this shell script!"
    echo -e '\n${V_SHELL_HOME}/hive_space.sh 20170101 '
}

# Check variable
if [[ -z ${1} ]];then
    USAGE
    exit 1
fi

# Define basic parm
V_SCRIPT_NAME=$(basename ${0} | cut -d'.' -f1)
V_OUT_LOGS="${V_SHELL_LOGS}/${V_SCRIPT_NAME}_${2}_${V_TIME_STAMP}_$$.log"

# Define the output flow
exec 4>&1               # screen output
exec 3>${V_OUT_LOGS}   # script output
exec 2>&3               # error output
exec 1>&3               # standard output
echo -e "[Begin: $(date +%F) $(date +%T)]"

# Get parm
V_DATA_DATE=${1}
#V_DATA_SRC_ORG=${2}

#create the folder if not exists
#if [[ ! -d "${V_SHELL_PARM}/${V_DATA_SRC_ORG}" ]];then
#    mkdir -p "${V_SHELL_PARM}/${V_DATA_SRC_ORG}"
#fi

# STG_SPACE,TIME
SPACE_STG=`hadoop fs -du  /user/hive/warehouse/stg.db|awk '{ SUM += $1 } END { print SUM/(1024*1024*1024)}'`
SPACE_STG=$(awk -v n=$SPACE_STG 'BEGIN{printf("%.2f", n)}')
echo $SPACE_STG
SPACE_STG_TIME=`echo -e "$(date +%F) $(date +%T)"`
echo $SPACE_STG_TIME

# ODS_SPACE,TIME
SPACE_ODS=`hadoop fs -du  /user/hive/warehouse/ods.db|awk '{ SUM += $1 } END { print SUM/(1024*1024*1024)}'`
SPACE_ODS=$(awk -v n=$SPACE_ODS 'BEGIN{printf("%.2f", n)}')
echo $SPACE_ODS
SPACE_ODS_TIME=`echo -e "$(date +%F) $(date +%T)"`
echo $SPACE_ODS_TIME


# Translate paramater and output $SPACE_STG_TIME
# echo "#SPACE_STG#=$SPACE_STG"                         >  "${V_SHELL_PARM}/${V_DATA_SRC_ORG}/firstpage_parm.txt"
# echo "#SPACE_STG_TIME#=$SPACE_STG_TIME"               >>  "${V_SHELL_PARM}/${V_DATA_SRC_ORG}/firstpage_parm.txt"
# echo "#SPACE_ODS#=${SPACE_ODS}"                       >>  "${V_SHELL_PARM}/${V_DATA_SRC_ORG}/firstpage_parm.txt"
# echo "#SPACE_ODS_TIME#=${SPACE_ODS_TIME}"             >>  "${V_SHELL_PARM}/${V_DATA_SRC_ORG}/firstpage_parm.txt"

V_QUERY="DELETE FROM ADM.HIVE_SPACE_TIME WHERE SUBSTR(SPACE_STG_TIME,1,10)=CONCAT(SUBSTR('${SPACE_STG}',1,4),'-',SUBSTR('${SPACE_STG}',6,2),'-',SUBSTR('${SPACE_STG}',9,2)); INSERT INTO ADM.HIVE_SPACE_TIME (SPACE_STG,SPACE_STG_TIME,SPACE_ODS,SPACE_ODS_TIME) SELECT '${SPACE_STG}','${SPACE_STG_TIME}','${SPACE_ODS}','${SPACE_ODS_TIME}' FROM DUAL ;"
${V_RUN_MYSQL} -q "${V_QUERY}"

if [[ $? -eq 0 ]];then
    echo -e "\nSuccess to INSERT INTO TABLE  ADM.HIVE_SPACE_TIME...\n"
else
    echo -e "\nCan not INSERT INTO TABLE  ADM.HIVE_SPACE_TIME."
    echo -e "\n${V_QUERY}"
    exit 1
fi
echo -e "\n[End: $(date +%F) $(date +%T)]"
exit 0
